package com.d16_Socket_Homework.homework5;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ServerDemo5_TCP {
    public static void main(String[] args) throws IOException {
        //创建接收端ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(1111);
        //自定义线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                10,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        //循环--有一个客户端请求，就建立一个连接
        while (true) {
            //创建Socket对象调用accpet方法接收连接
            Socket socket = serverSocket.accept();
            //建立一条连接就开一条线程
            ThreadSocket ts = new ThreadSocket(socket);
            //new Thread(ts).start();
            pool.submit(ts);
        }
        //serverSocket.close();

    }
}
